package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.EditorChief;
import com.ejuornal.dao.EditorChiefDao;

@Component("editorChiefDao")
public class EditorChiefDaoImpl extends DaoSupport<EditorChief> implements
		EditorChiefDao {

	public void add(String sql, Object... objs) {
		super.add(sql, objs);
	}

	public void batchDelete(String sql, int... ids) {
		super.batchDelete(sql, ids);
	}

	public void update(String sql, Object... objs) {
		super.update(sql, objs);
	}

	public EditorChief findById(int editorChiefId) {
		String sql = "select * from t_editor_chief where editorChiefId = ?";
		EditorChief editorChief = super.getJdbcTemplate().queryForObject(sql,
				new Object[] { editorChiefId }, new RowMapper<EditorChief>() {
					public EditorChief mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						EditorChief editorChief = new EditorChief();
						editorChief.setAddress(rs.getString("address"));
						editorChief.setBusinessId(rs.getString("businessId"));
						editorChief.setBusinessImg(rs.getString("businessImg"));
						editorChief.setCodeId(rs.getString("codeId"));
						editorChief.setCodeImg(rs.getString("codeImg"));
						editorChief.setEditorChiefId(rs.getInt("editorChiefId"));
						editorChief.setStatus(rs.getString("status"));
						editorChief.setUserId(rs.getInt("userId"));
						return editorChief;
					}
				});
		return editorChief;
	}

	public EditorChief findByUserId(int userId) {
		String sql = "select * from t_editor_chief where userId = ?";
		EditorChief editorChief = super.getJdbcTemplate().queryForObject(sql,
				new Object[] { userId }, new RowMapper<EditorChief>() {
					public EditorChief mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						EditorChief editorChief = new EditorChief();
						editorChief.setAddress(rs.getString("address"));
						editorChief.setBusinessId(rs.getString("businessId"));
						editorChief.setBusinessImg(rs.getString("businessImg"));
						editorChief.setCodeId(rs.getString("codeId"));
						editorChief.setCodeImg(rs.getString("codeImg"));
						editorChief.setEditorChiefId(rs.getInt("editorChiefId"));
						editorChief.setStatus(rs.getString("status"));
						editorChief.setUserId(rs.getInt("userId"));
						return editorChief;
					}
				});
		return editorChief;
	}

	@Override
	public boolean killChief(int userId) {
		String  sql=" UPDATE t_user  u set  u.roleId = 1  WHERE  u.userId =?  ";
		int num = getJdbcTemplate().update(sql, new Object[]{userId});
		if (num ==1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean recoverChief(int userId) {
		String sql="UPDATE t_user u set u.roleId = 4 WHERE u.userId=?";
		int num = getJdbcTemplate().update(sql, new Object[]{userId});
		if (num ==1) {
			return true;
		}
		return false;
	}
	
	@Override
	public boolean isApplied(int userId) {
		String sql = "select count(*) from t_editor_chief where userId=?";
		@SuppressWarnings("deprecation")
		int result = super.getJdbcTemplate().queryForInt(sql, new Object[]{ userId });
		if(result > 0) {
			return true;
		} else {
			return false;
		}
	}
}
